$(function () {

    verifyCode.Init();

});


var verifyCode = {
    slidingCode: null,
    currBtn: null,
    para: null,
    edition: "",
    Init: function () {
        var self = this;

        //$("#verificationCodeMask").css("opacity", 0.6);

        ////点击关闭验证码弹层
        //$(".closeMask").on("click", function () {
        //    //$("#verification-code").empty();
        //    $(".verityMask").hide();
        //});


        //$("#verificationCodeMask").click(function () {

        //    self.hideVerifyCode();

        //});
    },

    InitslidingCode: function () {

        var self = this;

        self.slidingCode = new slidingVerification({
            id: "verification-code", // 验证码DIV的id
            appId: 100000313, // appId
            businessSite: "dingzhi_roborderlist_limit", // businessSite
            width: '280px', // 滑块宽度，可选，填像素字符串，如'280px'，默认值'280px'
            height: '40px', // 滑块长度，可选，填像素字符串，如'40px'，默认值'40px'
            language: 'zh-CN', // 语种，默认‘zh-CN’（简体中文）
            chooseOpt: {
                width: '280px',// 选字图片宽度，可选，选字验证码 大图的宽度
                height: '200px', // 选字图片高度，可选，选字验证码 大图的高度 如果选字width，height不填，生成的图片将根据后端默认配置进行屏幕大小自适应；如果选字width，height填写了，需要先找 ldq刘丹青 进行配置响应大小的图片
                type: "pop" // "over"： 浮窗(默认)；"pop"：弹出
            },
            selectBeforeHandler: function () {  // 选字弹框出现时调用的方法，可选，~2.3.0中移除
                alert("this is selectBeforeHandler");
            },
            hideBgHandler: function () { // 选字弹框消失时调用的方法，可选，~2.3.0中移除
                alert("this is hideBgHandler");
            },
            /**
             * 状态监听函数
             * @param state 0：滑块验证码显示；1：选字验证码弹框出现；2：选字验证码弹框消失
             */
            stateChange: function (state) {  // ~2.3.0中添加
                if (state == 0) {
                    //console.log("滑块显示");
                    if ($(".cpt-drop-box").length > 1) {
                        $(".cpt-drop-box").eq(0).siblings().remove();
                    }
                    //$(".verityMask").show();

                    self.showVerifyCode();

                } else if (state == 1) {
                    //console.log("选字验证码弹框出现");
                    //$("#verification-code").empty();
                    //$(".verityMask").hide();
                    self.hideVerifyCode();

                } else if (state == 2) {
                    //console.log("选字验证码弹框消失");
                    //loadData();

                }
            },
            resultHandler: function (e) {
                //console.log("token:", e.token, 'e.checkState:', e.checkState, 'e.version', e.version);
                var verifyToken = e.token;
                var jsVersion = e.version;
                if (e.checkState == "success") {
                    //$("#verification-code").empty();

                    self.hideVerifyCode();

                    if (self.currBtn === null || self.currBtn === undefined || verifyCode.para === null || verifyCode.para === undefined) {

                        alert("验证失败，请重试");
                        self.showVerifyCode();
                        self.refresh();
                        return;
                    }

                    var requireDetailId = verifyCode.para.requireDetailId;
                    var platformUserId = verifyCode.para.platformUserId;
                    var platformProviderId = verifyCode.para.platformProviderId;

                    if (self.edition === "new") {
                        doSubmit(self.currBtn, requireDetailId, platformUserId, platformProviderId, verifyToken, jsVersion, verifyCode.para.requireID);
                    } else {

                        doSubmit(self.currBtn, requireDetailId, platformUserId, platformProviderId, "GrabOrder", verifyToken, jsVersion);
                    }

                    return;
                }

                self.slidingCode.refresh();
                return;
            }
        });


    },


    refresh: function () {
        var self = this;
        self.slidingCode.refresh();
    },

    showVerifyCode: function (type) {
        var self = this;

        if (self.slidingCode === null || self.slidingCode === undefined) {

            self.InitslidingCode();
        }

        $(".verityMask").show();
        //$("#verificationCodeMask").show();
        //$("#verificationCodeBox").show();

    },

    hideVerifyCode: function () {
        var self = this;
        $(".verityMask").hide();

        //$("#verificationCodeMask").hide();
        //$("#verificationCodeBox").hide();
    }

};